package com.example.agriculturalmanagement;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.agriculturalmanagement.adapter.DataInfoAdapter;
import com.example.agriculturalmanagement.databinding.ActivityDataManageBinding;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class DataManageActivity extends AppCompatActivity {
    private ActivityDataManageBinding binding;
    private String mName;
    private List<Kind> mAddList = new ArrayList<>();
    private List<Kind> mRemoveList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDataManageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mName = getIntent().getStringExtra("name");
        setSupportActionBar(binding.toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle(getString(R.string.app_name));
        }
        BmobQuery<Person> query = new BmobQuery<Person>();
        query.addWhereEqualTo("name", mName);
        //返回50条数据，如果不加上这条语句，默认返回10条数据
        query.setLimit(50);
        //执行查询方法
        query.findObjects(new FindListener<Person>() {
            @Override
            public void done(List<Person> object, BmobException e) {
                if (e == null) {
                    if (object == null || object.size() == 0) {
                        return;
                    }
                    Person person = object.get(0);
                    Type listType = new TypeToken<List<Kind>>() {}.getType();
                    mAddList = new Gson().fromJson(person.getAddKind(), listType);
                    mRemoveList = new Gson().fromJson(person.getRemoveKind(), listType);
                    DataInfoAdapter addAdapter = new DataInfoAdapter(DataManageActivity.this, null);
                    DataInfoAdapter removeAdapter = new DataInfoAdapter(DataManageActivity.this, null);
                    binding.addRecyclerView.setLayoutManager(new GridLayoutManager(DataManageActivity.this, 3));
                    binding.removeRecyclerView.setLayoutManager(new GridLayoutManager(DataManageActivity.this, 3));
                    binding.addRecyclerView.setAdapter(addAdapter);
                    binding.removeRecyclerView.setAdapter(removeAdapter);
                    addAdapter.setNewData(mAddList);
                    removeAdapter.setNewData(mRemoveList);

                } else {
                    Log.i("bmob", "失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }
        });
    }
}